package com.example.demo.common;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hxf
 */
@Setter
@Getter
public class R<T> {

    private int code;

    private String msg;

    private T data;

    private Map extra = new HashMap();

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R() {

    }


    public static R error() {
        return new R(RCode.ERROR.getCode(), RCode.ERROR.getDesc());
    }

    public static R error(String msg) {
        return new R(RCode.ERROR.getCode(), msg);
    }

    public static<T>  R error(String msg, T data) {
        return new R(RCode.ERROR.getCode(), msg, data);
    }

    public static<T> R error(T data) {
        return new R(RCode.ERROR.getCode(), RCode.ERROR.getDesc(), data);
    }

    public static R ok() {
        return new R(RCode.OK.getCode(), RCode.OK.getDesc());
    }

    public static<T>  R ok(T data) {
        return new R(RCode.OK.getCode(), RCode.OK.getDesc(), data);
    }

//    public static R ok(String msg) {
//        return new R(RCode.OK.getCode(), msg);
//    }

    public static<T>  R ok(String msg, T data) {
        return new R(RCode.OK.getCode(), msg, data);
    }




    public static R notLogin() {
        return new R(RCode.NOT_LOGIN.getCode(), RCode.NOT_LOGIN.getDesc());
    }

    public static R notLogin(String msg) {
        return new R(RCode.NOT_LOGIN.getCode(), msg);
    }

    public static R notAuth() {
        return new R(RCode.NOT_AUTH.getCode(), RCode.NOT_AUTH.getDesc());
    }

    public static R notAuth(String msg) {
        return new R(RCode.NOT_AUTH.getCode(), msg);
    }

    public R putExtra(String key, Object value) {
        this.extra.put(key, value);
        return this;
    }

    public R putExtra(HashMap<String, Object> extra) {
        this.extra.putAll(extra);
        return this;
    }


}
